//
//  ZYCProfileStudentManagementViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/8/21.
//

#import "ZYCProfileStudentManagementViewController.h"
#import "ZYCStudentManagementCell.h"
#import "ZYCProfileUserHeaderInfoCell.h"
#import "ZYCProfileModifyUserInfomationViewController.h"
#import "ZYCStudentManagementModel.h"
#import "ZYCProfileFaceStudentEntyAlbumViewController.h"
#import "SystemCamera.h"
#import "ZYCProfileUserInfoFooterView.h"
#import "ZYCCompleteBasicInformationViewController.h"

@interface ZYCProfileStudentManagementViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCProfileUserInfoFooterViewDelegate> 

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, copy) NSString *realName;
@property (nonatomic, copy) NSString *idCardNo;
@property (nonatomic, copy) NSString *className;
@property (nonatomic, copy) NSString *customizePath;


@end

@implementation ZYCProfileStudentManagementViewController

#pragma mark - 生命周期函数
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"学生管理";
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getStudentInfo];
}


#pragma mark - 网络请求
- (void)getStudentInfo{
    NSDictionary *params = @{
        @"id":self.studentId
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Profile_Post_StudentManagement requestService:RequestServiceSystem parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {

    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            ZYCStudentManagementModel *userInfo = [ZYCStudentManagementModel yy_modelWithDictionary:responseObject[@"body"]];
            self.realName = userInfo.realName;
            self.idCardNo = userInfo.idCardNo;
            self.className = userInfo.className;
            self.customizePath = userInfo.customizePath;
            [self.tableView reloadData];
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {

    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ZYCProfileUserHeaderInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCProfileUserHeaderInfoCell class]) forIndexPath:indexPath];
        cell.titleString = self.dataSource[indexPath.section];
        cell.avaterInfo = self.customizePath;
        return cell;
    }else{
        ZYCStudentManagementCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCStudentManagementCell class]) forIndexPath:indexPath];
        cell.titleString = self.dataSource[indexPath.section];
        if (indexPath.section == 1) {
            cell.contentString = self.realName;
        }else  if (indexPath.section == 2) {
            NSString *idCardSecret = [NSString stringWithFormat:@"%@**********%@",[self.idCardNo substringToIndex:4],[self.idCardNo substringFromIndex:self.idCardNo.length-4]];
            cell.contentString  = idCardSecret;
        }else  if (indexPath.section == 3) {
            cell.contentString = self.className;
        }
        return cell;
    }
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 3) {
        return 200;
    }
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 65.0;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    // 完善更多资料
    if (section == 3) {
        ZYCProfileUserInfoFooterView *footerView = [[ZYCProfileUserInfoFooterView alloc] init];
        footerView.titleString = @"完善更多资料";
        footerView.delegate = self;
        return footerView;;
    }
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 0) {
        ZYCWeakSelf
        [SystemCamera sharedSystemCamera].SelectImage = ^(UIImage *headerImage, NSInteger buttonIndex) {
            ZYCProfileFaceStudentEntyAlbumViewController *vc = [[ZYCProfileFaceStudentEntyAlbumViewController alloc] init];
            vc.headerImage = headerImage;
            vc.studentId = self.studentId;
            if (buttonIndex == 0) {
                vc.type = ZYCProfileFaceEntryViewTypePhoto;
            }else{
                vc.type = ZYCProfileFaceEntryViewTypeAlbum;
            }
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        [SystemCamera cameraWillShowInController:self];
    }
}


        
#pragma mark - ZYCProfileUserInfoFooterViewDelegate
- (void)profileUserInfoFooterViewDidMore{
    ZYCCompleteBasicInformationViewController *vc = [[ZYCCompleteBasicInformationViewController alloc] init];
    vc.studentId = self.studentId;
    [self.navigationController pushViewController:vc animated:YES];
}
- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource =  @[@"人脸:",@"姓名:",@"证件号:",@"班级:"];
    }
    return _dataSource;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight ) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCStudentManagementCell class] forCellReuseIdentifier:NSStringFromClass([ZYCStudentManagementCell class])];
        [_tableView registerClass:[ZYCProfileUserHeaderInfoCell class] forCellReuseIdentifier:NSStringFromClass([ZYCProfileUserHeaderInfoCell class])];
        
    }
    return _tableView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
